package com.apesource.bookBuy.view;

import com.apesource.bookBuy.bean.Admin;
import com.apesource.bookBuy.bean.Book;
import com.apesource.bookBuy.dao.DaoUtil;
import com.apesource.bookBuy.mapper.AdminMapper;
import com.apesource.bookBuy.mapper.BookMapper;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

/**
 * @version 1.0
 * @auther:暮雪
 * @date:2025/9/4
 */
@Path("/admin")
public class AdminController extends Controller {
    public void index() {
        if (getSessionAttr("admin") != null) {
            SqlSession sqlSession = DaoUtil.getSqlSession();
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);

            String bookName = getPara("bookName");
            String author = getPara("bookAuthor");

            List<Book> bookList = bookMapper.getBooksByAuthorAndBookName(author, bookName);
            System.out.println(bookList);
            setAttr("booklist", bookList);
            render("/adminHome.html", 0);
        } else redirect("/admin/login");
    }

    public void login() {
        set("errorInfo", null);
        render("/adminLogin.html");
    }

    public void execu_login() {
        SqlSession sqlSession = DaoUtil.getSqlSession();
        AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);

        String username = getPara("adminName");
        String adminPwd = getPara("adminPwd");

        Admin admin = adminMapper.selectByAdminNameAndPwd(username, adminPwd);
        if (admin != null) {
            setSessionAttr("admin", admin);
            redirect("/admin/index");
        } else {
            set("errorInfo", "账号或密码错误");
            render("/adminLogin.html", 0);
        }
        DaoUtil.close(sqlSession);
    }

    public void logout() {
        setSessionAttr("admin", null);
        redirect("/admin/login");
    }

    public void peopleInfo() {
        if (getSessionAttr("admin") != null) {
            render("/peopleInfo.html", 0);
        } else redirect("/admin/login");
    }

    public void register() {
        render("/registerAdmin.html");
    }

    public void execu_register() {
        SqlSession sqlSession = DaoUtil.getSqlSession();
        AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);

        String username = getPara("adminName");
        String password = getPara("adminPwd");
        String email = getPara("adminEmail");
        int adminSex = getInt("adminSex");
        Date adaminBir = getDate("adminBir");
        String adminAddr = getPara("adminAddr");
        Admin admin;
        admin = adminMapper.selectByEmail(email);
        if (admin != null) {
            set("emailError", "error");
            System.out.println(123);
            render("/registerAdmin.html", 0);
            return;
        }
        admin = adminMapper.findByAdminName(username);
        if (admin != null) {
            set("userError", "error");
            render("/registerAdmin.html", 0);
            return;
        }

        admin = new Admin();
        admin.setAdminName(username);
        admin.setAdminPwd(password);
        admin.setAdminEmail(email);
        admin.setAdminAddr(adminAddr);
        admin.setAdminBir(adaminBir);
        admin.setAdminSex(adminSex);

        int res = adminMapper.insertAdmin(admin);
        if (res > 0) {
            setSessionAttr("admin", admin);
            sqlSession.commit();
            redirect("/admin");
        } else renderText("注册失败");
        DaoUtil.close(sqlSession);
    }

    public void execu_update() {
        SqlSession sqlSession = DaoUtil.getSqlSession();
        AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);
        int adminId = getParaToInt("adminId");
        String username = getPara("adminName");
        String email = getPara("adminEmail");
        Date adaminBir = getDate("adminBir");
        int adminSex = getInt("adminSex");
        String adminAddr = getPara("adminAddr");
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        admin.setAdminName(username);
        admin.setAdminEmail(email);
        admin.setAdminBir(adaminBir);
        admin.setAdminSex(adminSex);
        admin.setAdminAddr(adminAddr);

        int res = adminMapper.updateAdminInfo(admin);
        if (res > 0) {
            setSessionAttr("admin", admin);
            sqlSession.commit();
            redirect("/admin/peopleInfo");
        } else renderText("修改错误");
        DaoUtil.close(sqlSession);
    }

    public void forPwd() {
        render("/AdminForget.html");
    }

    public void forghtPwd() {
        SqlSession sqlSession = DaoUtil.getSqlSession();
        AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);
        String adminEmail = getPara("adminEmail");
        Admin admin = adminMapper.selectByEmail(adminEmail);
        if (admin != null) {
            int code = new Random().nextInt(9000) + 1000;

            execu_forghtPwd(admin, code);
        } else {
            set("emailError", "该邮箱未被注册");
            render("/AdminForget.html",0);
        }
        DaoUtil.close(sqlSession);
    }

    public void execu_fgtPwd(Admin admin,int code){

    }

    public void execu_forghtPwd(Admin admin,int code) {
        if(admin==null){
            redirect("/admin/forPwd");
            return;
        }
        SqlSession sqlSession = DaoUtil.getSqlSession();
        AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class);
        String receivecode=getPara("code");
        String pwd=getPara("password");

        if(receivecode==null || pwd==null){
            render("/adminVerification.html");
        } else if (Integer.valueOf(receivecode)!=code) {
            System.out.println(222);
            set("codeError", "验证码错误");
            render("/adminVerification.html",0);
        }else {
            int res=adminMapper.updateAdminPwd(admin.getAdminId(),pwd);
            System.out.println(444);
            if (res > 0) {
                System.out.println(333);
                sqlSession.commit();
                redirect("/admin/login");
            }else renderText("修改失败");
        }

    }
}
